package com.liuyk.commonlib;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.liuyk.commonlib.activity.BaseActivity;
import com.liuyk.commonlib.web.RouterConfig;

/**
 * 加载web fragment
 * <p>
 * Created by liuyakui on 2020/7/7
 */
@Route(path = RouterConfig.WEB_ROUTER)
public class CommonWebFragmentActivity extends BaseActivity {
    private WebViewFragment webFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.common_activity_web_fragment);
        statusBarLight(R.color.white);
        initView();
    }

    private void initView() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        webFragment = new WebViewFragment();
        webFragment.setArguments(getExtras());
        transaction.add(R.id.web_contain, webFragment);
        transaction.commitAllowingStateLoss();
    }

    public Bundle getExtras() {
        return getIntent().getExtras();
    }

    protected String getRouterUrl() {
        return getIntent().getExtras() == null ? "" : getIntent().getExtras().getString("url");
    }

    @Override
    protected boolean isShowToolBar() {
        return false;
    }

    @Override
    public void onBackPressed() {
        if (webFragment.getWebView() != null && webFragment.getWebView().canGoBack()) {
            webFragment.getWebView().goBack();
        } else {
            super.onBackPressed();
        }
    }
}
